#!/usr/bin/python
# -*- coding: utf-8 -*-
import subprocess
def tags_flac(cue_info):
  for track in cue_info['tracks']:
    track_file = track['number']+" - "+track['title']+".flac"
    print "Tagging file "+track_file 
    command = ["metaflac", track_file]
    command.append("--set-tag=TRACKNUMBER="+track['number'])
    command.append("--set-tag=TITLE="+track['title'])
    
    #check artist
    if not 'artist' in track:
      if not 'album_artist' in cue_info:
        artist = "Unknown"
      else:
        artist = cue_info['album_artist']
    else:
      artist = track['artist']
    command.append("--set-tag=ARTIST="+artist)
    
    if 'album_artist' in cue_info:
      command.append("--set-tag=ALBUM ARTIST="+cue_info['album_artist'])

    if 'album' in cue_info:
      command.append("--set-tag=ALBUM="+cue_info['album'])
    
    if 'date' in cue_info:
      command.append("--set-tag=DATE="+cue_info['date'])
    
    if 'genre' in cue_info:
      command.append("--set-tag=GENRE="+cue_info['genre'])

    subprocess.call(command)
  
def tags_mp3(cue_info):
  for track in cue_info['tracks']:
    track_file = track['number']+" - "+track['title']+".mp3"
    print "Tagging file "+track_file 
    command = ["lame", "--add-id3v2"]
    command.append("--tn "+track['number'])
    command.append("--tt "+track['title'])
    
    #check artist
    if not 'artist' in track:
      if not 'album_artist' in cue_info:
        artist = "Unknown"
      else:
        artist = cue_info['album_artist']
    else:
      artist = track['artist']
    command.append("--ta "+artist)
    
#    if 'album_artist' in cue_info:
#      command.append("--set-tag=ALBUM ARTIST="+cue_info['album_artist'])

    if 'album' in cue_info:
      command.append("--tl "+cue_info['album'])
    
    if 'date' in cue_info:
      command.append("--ty "+cue_info['date'])
    
    if 'genre' in cue_info:
      command.append("--tg "+cue_info['genre'])
    
    command.append(track_file)
    subprocess.call(command)
